﻿Public Class Frm_Manajemen_User_edit
    Public Sub initDataEdit(ByVal username As String)
        ' get data from database
        Db.FlushCache()
        Db.Selects("*")
        Db.From("tbl_user")
        Db.Where("user", username)
        Connection.ExecuteToDataReader(Db.GetQueryString)
        If Connection.Read.HasRows Then
            With Connection.Read
                .Read()
                txt_nim.Text = .Item("nim").ToString
                txt_username.Text = .Item("user").ToString
                txt_password.Text = .Item("pass").ToString
                cmb_type.SelectedIndex = .Item("type").ToString
                cmb_type.Text = IIf(cmb_type.SelectedIndex = 1, "Admin", "Guest")
            End With
        End If
    End Sub
    Private Sub cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_cancel.Click
        txt_nim.Text = ""
        txt_username.Text = ""
        txt_password.Text = ""
        cmb_type.Text = "- Pilih salah satu -"
        Me.Close()
    End Sub
    Private Sub cmd_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_update.Click
        If txt_cPassword.Text <> txt_password.Text Then
            MsgBox("Cek Lagi Password yang Anda Masukan", MsgBoxStyle.Critical)
            txt_password.Focus()
        ElseIf txt_cPassword.Text = txt_password.Text Then
            Db.FlushCache()
            Db.Update("tbl_user")
            Db.SetField("user", txt_username.Text)
            'Db.SetField("pass", txt_password.Text)
            Db.SetField("nim", txt_nim.Text)
            Db.SetField("type", cmb_type.SelectedIndex)
            Db.Where("nim", txt_nim.Text)

            Dim query As String = vbNullString
            query &= " UPDATE tbl_user SET pass = MD5('" & txt_cPassword.Text & "') WHERE nim = '" & txt_nim.Text & "'"
            Connection.ExecuteNonQuery(query)

            If Connection.ExecuteNonQuery(Db.GetQueryString) Then
                frm_user_list.InitGrid()
                Me.Close()
            End If
        End If

    End Sub
   
End Class